<?php
require_once('./includes/headers.inc.php');
require_once('./top.php');

$author_id = (int)$_GET['id'];
$error = FALSE;
$user_link = '';

$author = $authorMgr->get($author_id);
if(!$author) {
    header('Location: /notfound.php');
    exit();
}

$locations = $locationMgr->getAll();
foreach($locations as $row) {
    if($row['id'] == $author['location_id']) {
        $location_info = '<a href="/viewlocation.php?id=' . $row['id'] . '">' . $row['name'] . '</a>';
        break;
    }
}

if($author['user_id']) {
    $user_link = '<li><a href="/viewuser.php?id=' . $author['user_id'] . '">Click for user page</a></li>';
}

$author_stats = $authorMgr->getStats($author_id);
$difficulties = $diffMgr->getAll();
foreach($difficulties as $row) {
    if($author_stats['min_diff'] == $row['value']) {
        $route_diff_min = $row['name'];
    }
    if($author_stats['max_diff'] == $row['value']) {
        $route_diff_max = $row['name'];
    }
    if($author_stats['avg_diff'] == $row['value']) {
        $route_diff_avg = $row['name'];
    }
}

$latest_routes = $authorMgr->getLatestRoutes($author_id, 10);
$colours = $colourMgr->getAll();
$latest_routes_info = array('<ul>');
foreach($latest_routes as $row) {
    $latest_routes_info[] = '<li>' . date('d-m-Y', strtotime($row['created'])) . ': <span class="colour ' . $colours[$row['colour_id']]['name'] . '"><a href="/viewroute.php?id=' . $row['id'] . '">' . $difficulties[$row['difficulty_id']]['name'] . ' ' . $colours[$row['colour_id']]['name'] . '</a></span> ' . ($row['unicode'] ? '<img class="routetype ' . $row['route_type'] . '" src="/img/pixel.png"> ' : '') . ($row['name'] ? '<i>' . $row['name'] . '</i>' : '') . '</li>';
}
$latest_routes_info[] = '</ul>';

$replaces = array(
    'name'                      => $author['name'],
    'location_info'             => $location_info,
    'user_link'                 => $user_link,
    //'route_firstcreated'        => date('d-m-Y', strtotime($author_stats['first_created'])),
    //'route_count'               => $author_stats['count'],
    //'route_diff_min'            => $route_diff_min,
    //'route_diff_max'            => $route_diff_max,
    //'route_diff_avg'            => $route_diff_avg,
    'latest_routes'             => implode("\n", $latest_routes_info),
    'error'                     => $msgMgr->formatMsg($error),
);
$lang = array(
    'header_authorinfo'         => $langMgr->get('header_authorinfo'),
    'header_statistics'         => $langMgr->get('header_statistics'),
    'text_author_firstcreated'  => $langMgr->get('text_author_firstcreated', false, array(
        'route_firstcreated'        => date('d-m-Y', strtotime($author_stats['first_created'])),
    )),
    'text_author_totalcreated'  => $langMgr->get('text_author_totalcreated', false, array(
        'route_count'               => $author_stats['count'],
    )),
    'text_author_diffcreated'   => $langMgr->get('text_author_diffcreated', false, array(
        'route_diff_min'            => $route_diff_min,
        'route_diff_max'            => $route_diff_max,
        'route_diff_avg'            => $route_diff_avg,
    )),
    'header_author_latest'      => $langMgr->get('header_author_latest'),
);
$templateMgr->loadContent(basename(__FILE__, '.php'), array_merge($replaces, $lang));
?>
